/*
 * Created by JFormDesigner on Mon Aug 28 15:38:49 CST 2023
 */

package src;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class addPage extends JFrame {
    public addPage() {
        initComponents();
    }

    public boolean textCheck(){
        boolean flag = true;
        String mcname = "^[\u4e00-\u9fa5]{2,10}$";
        String mcgrade = "\\d{4}$";
        String mcborn = "\\d{4}-\\d{2}-\\d{2}";
        if(!this.textFieldName.getText().matches(mcname)){
            flag =false;
            this.labelError.setText("姓名有误！(姓名应为纯中文！长度限制2-10)");
        }
        else if(!this.radioButtonMan.isSelected() && !this.radioButtonWomen.isSelected()) {
            flag = false;
            this.labelError.setText("请选择性别！");
        }
        else if(!this.textFieldGrade.getText().matches(mcgrade)){
            flag = false;
            this.labelError.setText("年级输入有误！检查年份（20xx）");
        }
        else if(!this.textFieldBorn.getText().matches(mcborn)){
            flag = false;
            this.labelError.setText("请检查出生日期！格式（yyyy-mm-dd）");
        }

        return flag;
    }

    public void clean(){
        this.textFieldName.setText("");
        this.textFieldPro.setText("");
        this.textFieldGrade.setText("");
        this.textFieldBorn.setText("");
        this.radioButtonMan.setSelected(false);
        this.radioButtonWomen.setSelected(false);
    }
    private void buttonCleanMouseClicked(MouseEvent e) {
        clean();

    }

    private void buttonBackMouseClicked(MouseEvent e) {
        this.dispose();
        new mainPage().setVisible(true);
    }

    private void buttonEnterMouseClicked(MouseEvent e) throws IOException {
        boolean flag = true;
        StudentData sd = new StudentData();
        sd.getFileDate();
            if (sd.isFull()) {
                flag = false;
                this.labelError.setText("学生容量已满！无法录入！");
                clean();
            } else {
                //检查信息

                if (flag && textCheck()) {
                    String sex = "";
                    if (this.radioButtonMan.isSelected() && !this.radioButtonWomen.isSelected()) sex = "男";
                    else if (!this.radioButtonMan.isSelected() && this.radioButtonWomen.isSelected()) sex = "女";
                    Student std = new Student(sd.createNumber(sd.stdsdata), this.textFieldName.getText(), sex, this.textFieldPro.getText(), this.textFieldGrade.getText(), this.textFieldBorn.getText());
                    sd.stdsdata[++sd.capacity] = std;//更新缓存区
                    sd.addFileData(std);//写入文件

                    this.labelError.setText(std.getName() + "同学(自动分配学号:" + std.getNumber() + ")的学籍录入成功！");
                    clean();
                }
            }
        }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        buttonBack = new JButton();
        label1 = new JLabel();
        textFieldName = new JTextField();
        textFieldPro = new JTextField();
        textFieldGrade = new JTextField();
        textFieldBorn = new JTextField();
        label2 = new JLabel();
        radioButtonMan = new JRadioButton();
        radioButtonWomen = new JRadioButton();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        buttonEnter = new JButton();
        buttonClean = new JButton();
        labelError = new JLabel();

        //======== this ========
        setTitle("添加页面");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- buttonBack ----
            buttonBack.setText("返回");
            buttonBack.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    buttonBackMouseClicked(e);
                }
            });
            panel1.add(buttonBack);
            buttonBack.setBounds(new Rectangle(new Point(295, 260), buttonBack.getPreferredSize()));

            //---- label1 ----
            label1.setText("姓名：");
            panel1.add(label1);
            label1.setBounds(25, 20, 40, label1.getPreferredSize().height);
            panel1.add(textFieldName);
            textFieldName.setBounds(75, 15, 205, textFieldName.getPreferredSize().height);
            panel1.add(textFieldPro);
            textFieldPro.setBounds(75, 95, 205, 30);
            panel1.add(textFieldGrade);
            textFieldGrade.setBounds(75, 140, 205, 30);
            panel1.add(textFieldBorn);
            textFieldBorn.setBounds(75, 190, 205, 30);

            //---- label2 ----
            label2.setText("性别：");
            panel1.add(label2);
            label2.setBounds(25, 60, label2.getPreferredSize().width, 15);

            //---- radioButtonMan ----
            radioButtonMan.setText("男");
            panel1.add(radioButtonMan);
            radioButtonMan.setBounds(new Rectangle(new Point(85, 55), radioButtonMan.getPreferredSize()));

            //---- radioButtonWomen ----
            radioButtonWomen.setText("女");
            panel1.add(radioButtonWomen);
            radioButtonWomen.setBounds(new Rectangle(new Point(145, 55), radioButtonWomen.getPreferredSize()));

            //---- label3 ----
            label3.setText("专业：");
            panel1.add(label3);
            label3.setBounds(new Rectangle(new Point(25, 100), label3.getPreferredSize()));

            //---- label4 ----
            label4.setText("年级：");
            panel1.add(label4);
            label4.setBounds(new Rectangle(new Point(25, 145), label4.getPreferredSize()));

            //---- label5 ----
            label5.setText("出生：");
            panel1.add(label5);
            label5.setBounds(new Rectangle(new Point(25, 195), label5.getPreferredSize()));

            //---- buttonEnter ----
            buttonEnter.setText("录入");
            buttonEnter.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    try {
                        buttonEnterMouseClicked(e);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
            panel1.add(buttonEnter);
            buttonEnter.setBounds(new Rectangle(new Point(75, 260), buttonEnter.getPreferredSize()));

            //---- buttonClean ----
            buttonClean.setText("重置");
            buttonClean.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    buttonCleanMouseClicked(e);
                }
            });
            panel1.add(buttonClean);
            buttonClean.setBounds(new Rectangle(new Point(190, 260), buttonClean.getPreferredSize()));
            panel1.add(labelError);
            labelError.setBounds(15, 225, 375, 27);
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 0, 420, 310);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radioButtonMan);
        buttonGroup1.add(radioButtonWomen);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton buttonBack;
    private JLabel label1;
    private JTextField textFieldName;
    private JTextField textFieldPro;
    private JTextField textFieldGrade;
    private JTextField textFieldBorn;
    private JLabel label2;
    private JRadioButton radioButtonMan;
    private JRadioButton radioButtonWomen;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JButton buttonEnter;
    private JButton buttonClean;
    private JLabel labelError;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
